package gatewinapp;


import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Thach
 */
public class CreateXML {
    private File file;
    private Document doc;
    
    public CreateXML(File file) throws ParserConfigurationException, SAXException, IOException
    {
        this.file = file;
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        this.doc = dBuilder.parse(this.file);
        
        this.doc.getDocumentElement().normalize();
    }
    
    public String getTextNode()
    {
        NodeList nodelist = this.doc.getElementsByTagName("TextWithNodes");
        String text = "";
         for(int i = 0; i < nodelist.getLength(); i++){
            org.w3c.dom.Node node = nodelist.item(i);
                            if(node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
                                Element element = (Element)node;
                               text += element.getTextContent();
                            }
        }
         return text;
    }
    
    public void removeAnnotationSet()
    {
        org.w3c.dom.Element element = (Element)this.doc.getElementsByTagName("TextWithNodes").item(0);
        element.getParentNode().removeChild(element);
        org.w3c.dom.NodeList nodelist1 = this.doc.getElementsByTagName("AnnotationSet");
            for(int i=0; i< nodelist1.getLength(); i++){
            org.w3c.dom.Element element1 = (Element)nodelist1.item(i);
            element1.getParentNode().removeChild(element1);
        }
    }
    
    public Element createElement(String name, String value)
    {
        Element element = this.doc.createElement(name);
        element.appendChild(this.doc.createTextNode(value));
        return element;
    }
    
    public void createElementResult(int frequency, String leftcontent, String keyword, String rightcontent)
    {
        Element gate = (Element)this.doc.getElementsByTagName("GateDocument").item(0);
        Element result = this.doc.createElement("result");
        gate.appendChild(result);
        
        result.appendChild(createElement("frequency",String.valueOf(frequency)));
        result.appendChild(createElement("leftcontent", leftcontent));
        result.appendChild(createElement("keyword", keyword));
        result.appendChild(createElement("rightcontent", rightcontent));
    }
    
    public void createFileResult() throws TransformerConfigurationException, TransformerException
    {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(this.doc);
        StreamResult kq = new StreamResult(new File("result/result_"+ this.file.getName().substring(0, this.file.getName().length() - 4)+".xml"));
        transformer.transform(source, kq);
    }
    
}
